package com.java2018.springmvc.book.controller;

import com.alibaba.fastjson.JSON;
import com.java2018.springmvc.author.model.Author;
import com.java2018.springmvc.author.service.AuthorService;
import com.java2018.springmvc.book.model.Book;
import com.java2018.springmvc.book.service.BookService;
import com.java2018.springmvc.utils.entity.JsonResponse;
import com.java2018.springmvc.utils.uuid.UUIDUtil;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    AuthorService authorService;

    @RequestMapping( value = "/get")

    public  ModelAndView  index() {
        List<Book> bookList = bookService.getAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("bookList",bookList);
        modelAndView.setViewName("book/index");
        return modelAndView;
    }

    @RequestMapping( value = "/add",method = RequestMethod.GET)
    public String addForm(Model model){
        List<Author> authorList = authorService.getAll();
        model.addAttribute("authorList",authorList);
        return "book/add";
    }


    @RequestMapping( value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse add(@RequestBody Book book) throws Exception {
        return bookService.add(book);

    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String deleteForm(){
        return "book/delete";
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public String delete(@PathVariable("id") String id,Book book){
        System.out.println(id);
        System.out.println(book);
        return "redirect:/book/delete";
    }
    @InitBinder
    public void initBinder(WebDataBinder binder) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
    @RequestMapping(value = "/",method = RequestMethod.POST)
    public String add2(Book book){
        System.out.println(book);
        return "redirect:/book/delete";
    }
    @ExceptionHandler(NumberFormatException.class)
    public String showError(Exception ex, Model model){
        System.out.println("局部异常捕获");
        model.addAttribute("exception",ex);
        return "error/error";
    }
    @RequestMapping(value = "/upload",method = RequestMethod.GET)
    public String uploadForm(){
        return "file/upload_form";
    }
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public String uploadFile(String desc, MultipartFile file,Model model) throws IOException {
        System.out.println(desc);

        if(file != null && StringUtils.isNotBlank(file.getOriginalFilename()) && file.getOriginalFilename().length()>0){
            String rootPath = System.getProperty("webapp.root");
            String storePath = new File(rootPath).getParentFile().getParent();
            String extra = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String fileName = UUIDUtil.getUUID()+extra;
            String filePath = storePath+"/upload/"+ fileName;
            File targetFile = new File(filePath);
            file.transferTo(targetFile);
            model.addAttribute("path","/file/"+fileName);


        }

        return "file/success";

    }






}
